var express = require('express');
var router = express.Router();

// 自定义模块
const query = require('../../utils/mysql');
const Result = require('../../utils/api');

var checkAuth = require('../../utils/checkAuth')
// 用户列表
router.post('/user/list', checkAuth, async function (req, res, next) {
    try {
        let params = req.body
        let sql = 'SELECT * FROM blog_user LIMIT ?,?'
        let start = (params.page - 1) * params.limit
        let end = params.limit
        let sqlArr = [start, end]
        let countSql = 'SELECT COUNT(*) AS count FROM blog_user'
        let userList = await query(sql, sqlArr)
        // console.log(userList);
        let [{
            count
        }] = await query(countSql)
        Result.success(res, {
            data: userList,
            count: count
        })
    } catch (error) {
        // console.log(error);
        Result.fail(res, '参数错误')
    }
})

/**
 * 配置信息修改
 */
router.post('/userInfo/save', checkAuth, async function (req, res, next) {
    let params = req.body
    let sql,
        sqlArr = [params.avatar, params.name, params.quotes, params.qq, params.netease_cloud, params.bilibili, params.gitee, params.bgimg, params.logo];
    if (!params.id) {
        sql = 'INSERT INTO blog_config (avatar,name,quotes,qq,netease_cloud,bilibili,gitee,bgimg,logo) VALUES (?,?,?,?,?,?,?,?,?)'
    } else {
        sql = 'UPDATE blog_config SET avatar=?,name=?,quotes=?,qq=?,netease_cloud=?,bilibili=?,gitee=?,bgimg=?,logo=? WHERE id=?'
        sqlArr = [...sqlArr, params.id]
    }
    query(sql, sqlArr).then(success => {
        Result.success(res, 'SUCCESS', '更新成功')
    }).catch(err => {
        Result.fail(res, '更新失败')
    })
})

module.exports = router;